package cn.linshu.security.jwt.controller;

import cn.linshu.security.jwt.dto.CommonResult;
import cn.linshu.security.jwt.dto.ResultCode;
import cn.linshu.security.jwt.dto.SystemLoginParam;
import cn.linshu.security.jwt.entity.SysResource;
import cn.linshu.security.jwt.entity.SysUser;
import cn.linshu.security.jwt.service.SysTokenUserService;
import cn.linshu.security.jwt.service.SysUserService;
import cn.linshu.security.jwt.service.SystemService;
import cn.linshu.security.jwt.service.impl.SysUserDetails;
import cn.linshu.security.jwt.util.Asserts;
import cn.linshu.security.jwt.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * -*- coding: utf-8 -*-
 *
 * @Author linnin
 * @Name springboot3-labs-master
 * @Time 2025/2/3 23:49
 * @Note
 * @For
 * @Out Copyright© 2025 linnin
 */

/* coding: utf-8 */

@RestController
@RequestMapping("/api/v1")
public class SystemController {
    @Autowired
    private SystemService systemService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysTokenUserService sysUserTokenService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    // 登录并返回token 但是不保存token
    @RequestMapping(value = "/login/no_save_token", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@Validated @RequestBody SystemLoginParam systemLoginParam) {
        String token = systemService.login(systemLoginParam.getUsername(), systemLoginParam.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("账户状态异常/账号或密码错误/用户不存在");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @RequestMapping(value = "/test")
    @ResponseBody
    public CommonResult test() {
        return CommonResult.success("test");
    }

    // 登录并返回token 并且保存token
    @RequestMapping(value = "/login/save_token", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult loginSaveToken(@Validated @RequestBody SystemLoginParam systemLoginParam) {
        SysUser userByUsername = sysUserService.findUserByUsername(systemLoginParam.getUsername());
        if (userByUsername == null) {
            Asserts.fail(ResultCode.USER_NOT_FOUND.getCode(), ResultCode.USER_NOT_FOUND.getMessage());
        }
        //组装用户
        List<SysResource> resourceList = new ArrayList<>();
        SysUserDetails sysUserDetails = new SysUserDetails(userByUsername, resourceList);
        if (!passwordEncoder.matches(systemLoginParam.getPassword(), sysUserDetails.getPassword())) {
            //密码不匹配
            Asserts.fail(ResultCode.PASSWORD_INCORRECT.getCode(), ResultCode.PASSWORD_INCORRECT.getMessage());
        }
        if (!sysUserDetails.isEnabled()) {
            //账号被禁用
            Asserts.fail(ResultCode.ACCOUNT_DISABLED.getCode(), ResultCode.ACCOUNT_DISABLED.getMessage());
        }
        //创建token
        String token = sysUserTokenService.createToken(sysUserDetails);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }
}
